package team.seekers.catering.food.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.seekers.catering.common.result.R;
import team.seekers.catering.common.result.ResultCode;
import team.seekers.catering.food.entity.dao.Product;
import team.seekers.catering.food.entity.dto.add.ProductAddDTO;
import team.seekers.catering.food.entity.dto.query.ProductQuery;
import team.seekers.catering.food.entity.dto.update.ProductUpdateDTO;
import team.seekers.catering.food.entity.vo.info.ProductInfoVO;
import team.seekers.catering.food.service.ProductService;

import java.util.Map;

@RestController
@RequestMapping("/goods")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 新增商品
     */
    @PostMapping("/add")
    public R add(@RequestBody ProductAddDTO productAddDTO){
        String id = productService.add(productAddDTO);
        return R.success().data("id",id);
    }

    /**
     * 删除商品
     */
    @GetMapping("/delete/{goodsId}")
    public R delete(@PathVariable("goodsId")String id){
        productService.removeById(id);
        return R.success();
    }

    /**
     * 修改套餐
     */
    @PostMapping("/update")
    public R update(@RequestBody ProductUpdateDTO productUpdateDTO){
        String id = productService.update(productUpdateDTO);
        return R.success().data("id",id);
    }

    /**
     * 分页查询所有商品
     */
    @PostMapping("/adminPageGoods/{current}/{limit}")
    public R adminPageGoods(@PathVariable("current") long current,
                            @PathVariable("limit") long limit,
                            @RequestBody ProductQuery query){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if(!StringUtils.isNullOrEmpty(query.getNameKey())){
            wrapper.like("name",query.getNameKey());
        }
        Map<String, Object> map = productService.adminPageGoods(current,limit,wrapper);
        if(map == null){
            return R.fail(ResultCode.GET_FAIL);
        }
        return R.success(map);
    }

    /**
     * 根据套餐id查询商品详情
     */
    @GetMapping("/query/{goodsId}")
    public R query(@PathVariable("goodsId")String id){
        ProductInfoVO productInfoVO = productService.get(id);
        return R.success().data("info", productInfoVO);
    }
}
